﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using zichan.BLL;
using zichan.Common;
namespace zichan.Controllers
{
    public class ExcelController :BaseController
    {
        // GET: Excel
        public ActionResult Index()
        {
            return View();
        }
        #region//资产分类
        public ActionResult Exc(string name,string identity)
        {
            ExportData(name, identity);
            return Json(new { jsonrpc = "2.0", err = 1, msg = "导出成功" });
        }
        private void ExportData(string name, string identity)
        {
            //string tableName = "";
            DataTable table = ListToDataTable(AsseteClassService.GetAsseteClassList(name, identity));
            string[] fields = { "SortId", "Name", "Identity","SubIdentity" };
            string[] fieldsName = { "序号", "名称", "标识(服务器品牌标识)","服务器组装标识" };
            ExcelHelper.Write(table, "资产分类表", "资产分类表", fields, fieldsName, "资产分类表.xls");
        }
        #endregion
        #region//库存管理列表
        public ActionResult AssetExc(string code, string userName, string name, string remarkKeyword, string adminName, int? assetClassId, int? serverPurposeId, int? companyId, int? statu, DateTime? sdt, DateTime? edt)
        {
            ExportData(code, userName, name, remarkKeyword, adminName, assetClassId, serverPurposeId, companyId, statu, sdt, edt);
            return Json(new { jsonrpc = "2.0", err = 1, msg = "导出成功" });
        }
        private void ExportData(string code, string userName, string name, string remarkKeyword, string adminName, int? assetClassId,int? serverPurposeId, int? companyId, int? statu, DateTime? sdt, DateTime? edt)
        {
            //string tableName = "";
            DataTable table = ListToDataTable(RepertoryService.List(code,userName,name,remarkKeyword,adminName,assetClassId, serverPurposeId,companyId, statu,sdt,edt));
            string[] fields = { "Code", "AssetClassName", "Name", "CompanyName", "UserName","StatusName" };
            string[] fieldsName = { "资产编号", "资产分类", "资产名称", "所属公司", "使用者", "状态" };
            ExcelHelper.Write(table, "库存表", "库存表", fields, fieldsName, "库存表.xls");
        }
        #endregion
        #region//机房管理
        public ActionResult MachineRoomExc(string name, int? MachineRoomClassId)
        {
            ExportData(name, MachineRoomClassId);
            return Json(new { jsonrpc = "2.0", err = 1, msg = "导出成功" });
        }
        private void ExportData(string name, int? MachineRoomClassId)
        {
            //string tableName = "";
            DataTable table = ListToDataTable(MachineRoomService.List(name,MachineRoomClassId));
            string[] fields = { "Name", "MachineRoomClassName" };
            string[] fieldsName = { "机房名称", "机房分类" };
            ExcelHelper.Write(table, "机房管理表", "机房管理表", fields, fieldsName, "机房管理表.xls");
        }
        #endregion
        #region//Ip管理
        public ActionResult IpExc(string Ip, int? MachineRoomId,int? statu)
        {
            ExportData(Ip, MachineRoomId,statu);
            return Json(new { jsonrpc = "2.0", err = 1, msg = "导出成功" });
        }
        private void ExportData(string Ip, int? MachineRoomId,int? statu)
        {
            //string tableName = "";
            DataTable table = ListToDataTable(IPService.List(Ip, MachineRoomId,statu));
            string[] fields = { "Ip", "MachineRoomName","statuName" };
            string[] fieldsName = { "IP", "机房名称","状态" };
            ExcelHelper.Write(table, "IP管理表", "IP管理表", fields, fieldsName, "IP管理表.xls");
        }
        #endregion
        #region//机柜管理
        public ActionResult CabinetExc(string name, string Ip, int? machineRoomId, int? cabinetClassId,int? serverPurposeSubId, string keyWord, string code, string uNO, int? vacancy, int? companyId, int? statu,int? cabinetId,int? isNull)
        {
            ExportData(name,Ip,machineRoomId,cabinetClassId, serverPurposeSubId,keyWord, code,uNO,vacancy,companyId,statu, cabinetId, isNull);
            return Json(new { jsonrpc = "2.0", err = 1, msg = "导出成功" });
        }
        private void ExportData(string name, string Ip, int? machineRoomId, int? cabinetClassId,int? serverPurposeSubId, string keyWord, string code, string uNO, int? vacancy, int? companyId, int? statu,int? cabinetId,int? isNull)
        {
            //string tableName = "";
            DataTable table = ListToDataTable(CabinetManageService.List(name, Ip, machineRoomId, cabinetClassId,serverPurposeSubId, keyWord, code, uNO, vacancy, companyId, statu, cabinetId, isNull));
            string[] fields = { "CabinetName", "UseNumber", "Vacancy", "MachineRoomName", "CompanyName", "serverPurposeName", "serverPurposeSubName", "Code", "Ip", "SubIP", "StatuName" };
            string[] fieldsName = { "机柜名称", "占位","空位","所属机房","所属公司","用途", "详细用途", "服务器编码","Ip","副Ip","状态" };
            ExcelHelper.Write(table, "机柜管理表", "机柜管理表", fields, fieldsName, "机柜管理表.xls");
        }
        #endregion
        #region 资产统计
        public ActionResult AssetStatisticsExc(int? assetClassId,int? serverPurposeId, int? companyId, DateTime? sdt, DateTime? edt)
        {
            ExportData(assetClassId, serverPurposeId,companyId, sdt,edt);
            return Json(new { jsonrpc = "2.0", err = 1, msg = "导出成功" });
        }
        private void ExportData(int? assetClassId,int? serverPurposeId, int? companyId, DateTime? sdt, DateTime? edt)
        {
            //string tableName = "";
            DataTable table = ListToDataTable(StatisticsService.AssetStatisticsList(assetClassId, serverPurposeId,companyId, sdt, edt));
            string[] fields = { "Name", "RepertoryNumber", "InuseNumber", "ScrapNumber", "TotalNumber" };
            string[] fieldsName = { "资产分类", "库存", "使用中", "报废", "总数"};
            ExcelHelper.Write(table, "资产统计表", "资产统计表", fields, fieldsName, "资产统计表.xls");
        }
        #endregion
        #region Ip统计
        public ActionResult IpStatisticsExc(int? machineRoomId)
        {
            IpExportData(machineRoomId);
            return Json(new { jsonrpc = "2.0", err = 1, msg = "导出成功" });
        }
        private void IpExportData(int? machineRoomId)
        {
            //string tableName = "";
            DataTable table = ListToDataTable(StatisticsService.IpStatisticsList(machineRoomId));
            string[] fields = { "MachineRoomName", "UsedIp", "SubIp", "TotalIp" };
            string[] fieldsName = { "机房名称", "已使用Ip数", "剩余Ip数", "Ip总数" };
            ExcelHelper.Write(table, "Ip统计表", "Ip统计表", fields, fieldsName, "Ip统计表.xls");
        }
        #endregion
        #region 机柜统计
        public ActionResult CabinetStatisticsExc( int? machineRoomId)
        {
            ExportData(machineRoomId);
            return Json(new { jsonrpc = "2.0", err = 1, msg = "导出成功" });
        }
        private void ExportData(int? machineRoomId)
        {
            //string tableName = "";
            DataTable table = ListToDataTable(StatisticsService.CabinetStatisticsList(machineRoomId));
            string[] fields = { "MachineRoomName", "CabinetName", "UsedNumber", "SubNumber", "TotalNumber" };
            string[] fieldsName = { "机房名称", "机柜名称", "已使用机架数", "剩余机架数", "机架总数"};
            ExcelHelper.Write(table, "机柜统计表", "机柜统计表", fields, fieldsName, "机柜统计表.xls");
        }
        #endregion
        public static DataTable ListToDataTable(IList list)
        {
            DataTable table = new DataTable();
            if (list.Count > 0)
            {
                PropertyInfo[] propertys = list[0].GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    Type pt = pi.PropertyType;
                    if ((pt.IsGenericType) && (pt.GetGenericTypeDefinition() == typeof(Nullable<>)))
                    {
                        pt = pt.GetGenericArguments()[0];
                    }
                    table.Columns.Add(new DataColumn(pi.Name, pt));
                }

                for (int i = 0; i < list.Count; i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in propertys)
                    {
                        object obj = pi.GetValue(list[i], null);
                        tempList.Add(obj);
                    }
                    object[] array = tempList.ToArray();
                    table.LoadDataRow(array, true);
                }
            }
            return table;
        }
    }
}